package com.itheima.travel.web;

import com.itheima.travel.enums.StatusEnum;
import com.itheima.travel.exception.ProjectException;
import com.itheima.travel.req.UserVo;
import com.itheima.travel.res.ResponseWrap;
import com.itheima.travel.service.UserService;
import com.itheima.travel.utils.EmptyUtil;
import com.itheima.travel.utils.ExceptionsUtil;
import com.sun.org.apache.xpath.internal.operations.Bool;
import jdk.net.SocketFlow;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 *
 */
@RestController // 设置当前类中所有方法的返回值都要转成json字符数据返回给浏览器
@Log4j2
@RequestMapping("/user") // 窄化路径
@CrossOrigin // 用于跨域访问资源
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private HttpSession session;

    // 注册
    @PostMapping("/register")
    // 响应封装
    public ResponseWrap<Boolean> registerUser(@RequestBody UserVo userVo) throws ProjectException {
        try {
            // 调用Service处理业务逻辑
            Boolean flag = userService.registerUser(userVo);
            // 封装返回结果对象
            ResponseWrap<Boolean> responseWrap = ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .data(flag)
                    .operationTime(new Date())
                    .build();
            return responseWrap;
        }catch (Exception e){
            // 将错误信息转成字符串,记录到错误日志中
            log.error("用户注册:{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.REGISTER_USER_FAIL.getCode(),StatusEnum.REGISTER_USER_FAIL.getMsg());
        }
    }

    @PostMapping("/login")
    public ResponseWrap<UserVo> loginUser(@RequestBody UserVo userVo) throws ProjectException {
        try{
            UserVo resultUserVo = userService.loginUser(userVo);
            // 封装返回结果
            ResponseWrap<UserVo> responseWrap = ResponseWrap.<UserVo>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .data(resultUserVo)
                    .operationTime(new Date())
                    .build();
            return responseWrap;
        } catch (Exception e){
            // 将错误信息转成字符串,记录到错误的日志中
            log.error("用户登陆:{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.LOGIN_USER_FAIL.getCode(),StatusEnum.LOGIN_USER_FAIL.getMsg());
        }

    }

    @GetMapping("/logout")
    public ResponseWrap<Boolean> loginOutUser() throws ProjectException {
        Boolean flag = true;
        try{
            //  调用方法退出登陆
            userService.loginOutUser();
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("退出业务:{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FAIL.getCode(),StatusEnum.FAIL.getMsg());
        }
    }

    /**
     * 用户是否登陆
     */
    @GetMapping("/islogin")
    public ResponseWrap<Boolean> isLogin() throws ProjectException {
        try{
            Boolean flag = userService.isLogin();
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("是否登陆:{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FAIL.getCode(),StatusEnum.FAIL.getMsg());
        }
    }

    /**
     * 使用session获取当前登陆用户
     */

    @RequestMapping("/currentUser")
    public ResponseWrap<UserVo> currentUser() throws ProjectException {
        try {
            Object sessionAttribute = session.getAttribute(session.getId());
            if (!EmptyUtil.isNullOrEmpty(sessionAttribute)){
                return ResponseWrap.<UserVo>builder()
                        .code(StatusEnum.SUCCEED.getCode())
                        .msg(StatusEnum.SUCCEED.getMsg())
                        .operationTime(new Date())
                        .data((UserVo)sessionAttribute)
                        .build();
            }else {
                return ResponseWrap.<UserVo>builder()
                        .code(StatusEnum.SUCCEED.getCode())
                        .msg(StatusEnum.SUCCEED.getMsg())
                        .operationTime(new Date())
                        .data(null)
                        .build();
            }
        }catch (Exception e){
            log.error("获取当前登陆的用户信息:{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FAIL.getCode(),StatusEnum.FAIL.getMsg());
        }
    }
}
